<?php
namespace App\Model\Attend;

use App\Model\BaseModel;

class Leavelog extends BaseModel
{
	public $tableName = "sc_leave_log";


	public function getLeaveLog(string $field = '*', array $where = [], array $order = ['id', 'desc'], $swhere){
		$result = $this
            ->field($field)
            ->order($order[0], $order[1])
            ->where($where)
            ->where($swhere)
            ->all();

        $lists = $this->lastQueryResult()->getResult();

        return $lists;
	}

	public function getLeaveStaff(string $field = '*', array $where = []){
        $result = $this
            ->field($field)
            ->alias('A')
            ->join('mymk_face as B', 'A.faceid=B.id', 'LEFT')
            ->where($where)
            ->get();
        $result = $this->lastQueryResult()->getResult();
        if(empty($result)){
            return false;
        }else{
            return $result[0];
        }
    }

    /**
     *获取员工的请假列表
     */
    public function getLeaveStaffPage($field = '*', $where = [],  $page = 1, $pageSize =10, $order = ['A.id', 'desc']){
        $this->withTotalCount()
            ->field($field)
            ->order($order[0], $order[1])
            ->alias('A')
            ->join('mymk_face as B','A.faceid=B.id','LEFT')
            ->where($where)
            ->page($page, $pageSize)
            ->all();


        $count = $this->lastQueryResult()->getTotalCount();
        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return ['count' => $count, 'lists' => $lists];
    }

    /**
     *获取主管审批列表
     */
    public function getLeaveAuthPage($field = '*', $where = [],  $page = 1, $pageSize =10, $order = ['A.id', 'desc']){
        $this->withTotalCount()
            ->field($field)
            ->order($order[0], $order[1])
            ->alias('A')
            ->join('mymk_leave_auth as B','A.id=B.logid','LEFT')
            ->where($where)
            ->page($page, $pageSize)
            ->all();


        $count = $this->lastQueryResult()->getTotalCount();
        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return ['count' => $count, 'lists' => $lists];
    }

    /**
     *获取审批详情数据
    */
    public function getLeaveDep(string $field = '*', array $where = []){
        $result = $this
            ->field($field)
            ->alias('A')
            ->join('mymk_face as B', 'A.faceid=B.id', 'LEFT')
            ->join('mymk_department as C', 'B.departmentid=C.id', 'LEFT')
            ->where($where)
            ->get();
        $result = $this->lastQueryResult()->getResult();
        if(empty($result)){
            return false;
        }else{
            return $result[0];
        }
    }

    /**
     * 获取主管未审批个数
     * @param string $field
     * @param array $where
     * @param int $page
     * @param int $pageSize
     * @param string[] $order
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function getLeaveUnauthCount($where = []){
        $this->alias('A')
            ->join('mymk_leave_auth as B','A.id=B.logid','LEFT')
            ->where('B.status',2)
            ->where('A.status',1)
            ->where($where)
            ->count('B.id');

        $count = $this->lastQueryResult()->getResultColumn('B.id')[0];

        return $count;
    }
}